//
//  SShoucangViewController.m
//  News
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SShoucangViewController.h"
#import "SDataBaseManager.h"
#import "SReadshoucang.h"
#import "SYueduViewController.h"
#import "UIImageView+WebCache.h"
#import "SDianyingBaseManager.h"
#import "Sshoucang.h"
#import "SFDetailsViewController.h"
@interface SShoucangViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property (nonatomic, retain) UISegmentedControl *seg;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *DianArr;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIAlertView *al;

@property (nonatomic, assign) NSInteger row;
@property (nonatomic, retain) NSIndexPath *indexpath;
@end

@implementation SShoucangViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
     self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor: [UIColor whiteColor], UITextAttributeFont:[UIFont boldSystemFontOfSize:20]};
    self.arr = [NSMutableArray array];
    self.DianArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.title = @"收藏";
    [self yemakongzhiqi];
    [self jianlitableView];
    [self getData];
    [self getData1];
    //当没有收藏时提示内容
    self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"818E8E9F-2BBE-46B2-9AD6-325395C6283F"];
    
    
            self.al = [[UIAlertView alloc] initWithTitle:nil message:@"您确定要删除" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                [self.view addSubview:_al];
    if (_DianArr.count == 0) {
        
        [self.view addSubview:_imageView];
    }
    else if (_DianArr.count != 0){
        [_imageView removeFromSuperview];
    }
    
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui"] style: UIBarButtonItemStylePlain target:self action:@selector(fanhui)];

   
    
}

- (void)fanhui
{
    [self.navigationController popViewControllerAnimated:YES];
}
//获取电影数据
- (void)getData
{
    SDianyingBaseManager *dbManager = [SDianyingBaseManager shareInstance];
    [dbManager openDB];
    NSArray *arr = [dbManager selectInfo];
    for (SDianyingBaseManager *shoucang in arr) {
        [_DianArr addObject:shoucang];
        
        
    }
  
}

/**
 *  页码控制器
 */
- (void)yemakongzhiqi
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"电影", @"文本"]];
    _seg.frame = CGRectMake(100, 64, self.view.frame.size.width - 200, 30);
    //默认选中索引
    _seg.selectedSegmentIndex = 0;
    //渲染颜色
    _seg.tintColor = [UIColor cyanColor];
    [_seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _seg;
    
    [_seg release];
    
    
}
/**
 *  页码控制器点击
 */
- (void)segAction:(UISegmentedControl *)seg
{
    self.i = seg.selectedSegmentIndex;
    if (_i == 1) {
      
    
        if (_arr.count != 0) {
            [_imageView removeFromSuperview];
        }

    }
    if (_i == 0 && _DianArr.count == 0) {
        [self.view addSubview:_imageView];
    }
    
    [_tableView reloadData];
}
/**
 *  建立tableView
 */
- (void)jianlitableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
}

- (void)getData1
{
    SDataBaseManager *dbManager = [SDataBaseManager shareInstance];
    [dbManager openDB];
    NSArray *arr = [dbManager selectInfo];
    for (SReadshoucang *shoucang in arr) {
        [_arr addObject:shoucang];
       
    }
   }
/**
 *  实现协议方法
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_i == 0) {
        return _DianArr.count;
    }
    else if (_i == 1){
        
        return _arr.count;
    }else{
    
    return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"str"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"str"];
    }
    
    if (_i == 0) {
       
        Sshoucang *shoucang = _DianArr[indexPath.row];
        cell.textLabel.text = shoucang.Mytitle;
        
    }
        if (_i == 1) {
        SReadshoucang *shoucang = _arr[indexPath.row];
        cell.textLabel.text = shoucang.title;
        cell.textLabel.numberOfLines = 0;
        //[cell.imageView sd_setImageWithURL:[NSURL URLWithString:shoucang.src]];
    
    }
        return cell;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_i == 1) {
        
        SYueduViewController *yuedu = [[[SYueduViewController alloc] init] autorelease];
        SReadshoucang *shoucang = _arr[indexPath.row];
        yuedu.biaoti = shoucang.title;
        if ([shoucang.ref isEqual:[NSNull null]]) {
            
            NSString *str = [NSString stringWithFormat:@"<img width=%lf src=\"%@\">", self.view.bounds.size.width - 20,shoucang.src];
            yuedu.xiangxi = [shoucang.body stringByReplacingOccurrencesOfString:shoucang.ref withString:str];
        }
        
      else  {
          //yuedu.biaoti = shoucang.title;
          
          //yuedu.xiangxi = [shoucang.body stringByAppendingString:shoucang.title];
          NSString *str = [NSString stringWithFormat:@"<h1>%@</h1>", shoucang.title];
          
          yuedu.xiangxi = [str stringByAppendingString:shoucang.body];
        }
        
       
        [self.navigationController pushViewController:yuedu animated:YES];
    }
    
    
    if (_i == 0) {
        
        SFDetailsViewController *dianyin = [[[SFDetailsViewController alloc] init] autorelease];
        Sshoucang *shoucang = _DianArr[indexPath.row];
        dianyin.large = shoucang.large;
        dianyin.xiamgqing = shoucang.xiamgqing;
        dianyin.pingfen = shoucang.pingfen;
        dianyin.renshu = shoucang.renshu;
        dianyin.leixing = shoucang.leixing;
        dianyin.shichang = shoucang.shichang;
        dianyin.guojia = shoucang.guojia;
        dianyin.Mytitle = shoucang.Mytitle;
        [self.navigationController pushViewController:dianyin animated:YES];
        
    
    }
    
    
    
    
}

#warning 开启编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [_tableView setEditing:editing animated:animated];
    
}
#warning 是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#warning 删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.row = indexPath.row;
    self.indexpath = indexPath;
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if (_i == 1) {
            
            [_al show];
        }
        if (_i == 0) {
            
            

           [_al show];
            

    }
        
    }
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (_i == 0) {
    if (buttonIndex == 0) {
        SDianyingBaseManager *dbManager = [SDianyingBaseManager shareInstance];
        [dbManager openDB];
        Sshoucang *shoucang = _DianArr[_row];
        [dbManager deleteInfoWithTitle:shoucang.Mytitle];
        [_DianArr removeObjectAtIndex:_row];
        NSArray *arr1 = @[_indexpath];
        [_tableView deleteRowsAtIndexPaths:arr1 withRowAnimation:UITableViewRowAnimationRight];
        if (_DianArr.count == 0) {
            
            [self.view addSubview:_imageView];
        }
    }}
    
    if (_i == 1) {
      if (buttonIndex == 0) {
        SDataBaseManager *dbManager = [SDataBaseManager shareInstance];
        [dbManager openDB];
        SReadshoucang *shoucang = _arr[_row];
        [dbManager deleteInfoWithTitle:shoucang.title];
        [_arr removeObjectAtIndex:_row];
        NSArray *arr = @[_indexpath];
        [_tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
        if (_arr.count == 0) {
            
            [self.view addSubview:_imageView];
        }

        
      }}
    
    if (buttonIndex == 1) {
        _tableView.editing = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
